package com.example.lockandqueue;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 无界阻塞队列
 */
public class LinkedBlockingQueueMain {

    LinkedBlockingQueue queue = new LinkedBlockingQueue<String>();

    public LinkedBlockingQueue getQueue() {
        return queue;
    }

    public void setQueue(LinkedBlockingQueue queue) {
        this.queue = queue;
    }

    public static void main(String[] args) {
        LinkedBlockingQueueMain linkedBlockingQueueMain = new LinkedBlockingQueueMain();
        LinkedBlockingQueue queue = linkedBlockingQueueMain.getQueue();
        new Thread(()->{
            while (true){
                int i = new Random().nextInt();
                queue.offer(i +"");
                System.out.println("<---"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            while (true){
                int size = queue.size();
                if(size>0){
                    Object poll = queue.poll();
                    System.out.println("--->"+poll);
                }
            }
        }).start();


    }

}
